﻿using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;

namespace XKPlatform.Common
{
    public class JsonHelper
    {

        /// <summary>
        /// 适用于没有新建实体之间
        /// </summary>
        static public T RotationMapping<T, S>(T t, S s)
        {
            var originalObj = s.GetType();
            var targetObj = typeof(T);
            foreach (PropertyInfo sp in originalObj.GetProperties())
            {
                foreach (PropertyInfo dp in targetObj.GetProperties())
                {
                    if (dp.Name == sp.Name)
                    {
                        dp.SetValue(t, sp.GetValue(s, null), null);
                    }
                }
            }
            return t;
        }

        /// <summary>
        /// 给对象赋值的方法(不赋地址)(同一个类型),含过滤
        /// </summary>
        /// <typeparam name="T"><peparam>
        /// <param name="left">=号左边</param>
        /// <param name="right">=号右边</param>
        /// <param name="id">过滤条件</param>
        public static void Assignment<T>(T left, T right, string id = null)
        {
            Type type = left.GetType();
            List<PropertyInfo> pList = type.GetProperties().ToList();
            for (int i = 0; i < pList.Count; i++)
            {
                //根据属性名获得指定的属性对象
                PropertyInfo gc = type.GetProperty(pList[i].Name);
                //设置属性的值
                if (id != null)
                {
                    if (pList[i].Name != id)
                    {
                        gc.SetValue(left, pList[i].GetValue(right, null), null);
                    }
                }
                else
                {
                    gc.SetValue(left, pList[i].GetValue(right, null), null);
                }

            }
        }
        public static DataTable CreateTable(Type t)
        {
            return BuiltTable(t.GetProperties());
        }
        private static DataTable BuiltTable(PropertyInfo[] pinfo)
        {
            try
            {
                if (pinfo == null) return null;
                DataTable table = new DataTable();
                foreach (PropertyInfo info in pinfo)
                {
                    Type type = info.PropertyType;
                    if (info.PropertyType.IsGenericType)
                        type = info.PropertyType.GetGenericArguments()[0];
                    DataColumn column = new DataColumn(info.Name, type);
                    column.AllowDBNull = true;
                    table.Columns.Add(column);
                }
                return table;
            }
            catch { return null; }
        }
        public static List<T> ToList<T>(string json)
        {
            List<T> list = JsonConvert.DeserializeObject<List<T>>(json);
            return list;
        }
        public static DataTable ToDataTable<T>(string json)
        {
            List<T> list = JsonConvert.DeserializeObject<List<T>>(json);

            var props = typeof(T).GetProperties();
            var dt = new DataTable();

            foreach (PropertyInfo pi in props)
            {
                Type colType = pi.PropertyType; if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                {
                    colType = colType.GetGenericArguments()[0];
                }
                dt.Columns.Add(new DataColumn(pi.Name, colType));
            }

            //dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray());
            if (list.Count() > 0)
            {
                for (int i = 0; i < list.Count(); i++)
                {
                    ArrayList tempList = new ArrayList();
                    foreach (PropertyInfo pi in props)
                    {
                        object obj = pi.GetValue(list.ElementAt(i), null);
                        tempList.Add(obj);
                    }
                    object[] array = tempList.ToArray();
                    dt.LoadDataRow(array, true);
                }
            }
            return dt;
        }

        
    }
}